package com.zhang.controller.member;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.member.entity.MemberReceiveAddress;
import com.zhang.member.service.IMemberReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址控制器
 *
 * @author hongmizfb
 * @date 2023/11/27
 */
@RestController
@RequestMapping("/memberReceiveAddress")
@SaCheckLogin
public class MemberReceiveAddressController {
    @Autowired
    private IMemberReceiveAddressService memberReceiveAddressService;

    /**
     * 获取收货地址列表
     *
     * @return {@link R}
     */
    @GetMapping("get/list")
    public R getList() {
        long userId = StpUtil.getLoginIdAsLong();
        List<MemberReceiveAddress> list = memberReceiveAddressService.listByUserId(userId);
        return RUtils.successData(list);
    }

    /**
     * 获取用户的默认收货地址
     *
     * @return {@link R}
     */
    @GetMapping("get/default")
    public R getDefaultAddress() {
        Long userId = StpUtil.getLoginIdAsLong();
        MemberReceiveAddress memberReceiveAddress = memberReceiveAddressService.getDefaultByUserId(userId);
        return RUtils.successData(memberReceiveAddress);
    }

    /**
     * 添加
     *
     * @param memberReceiveAddress
     * @return {@link R}
     */
    @PostMapping("add")
    R add(@RequestBody MemberReceiveAddress memberReceiveAddress) {
        long userId = StpUtil.getLoginIdAsLong();
        memberReceiveAddress.setMemberId(userId);
        return memberReceiveAddressService.addOne(memberReceiveAddress);
    }

    /**
     * 批量删除
     *
     * @param addressIds
     * @return {@link R}
     */
    @DeleteMapping("del/list")
    R delByUserId(@RequestBody List<Long> addressIds) {
        long userId = StpUtil.getLoginIdAsLong();
        memberReceiveAddressService.delByIds(addressIds);
        return RUtils.successMsg("删除成功");
    }
}
